﻿#include "tablemodel.h"

TableModel::TableModel(QObject *parent):QAbstractTableModel (parent)
{
}

void TableModel::SetData(const QVariantMap &map)
{
    //重置model数据之前调用beginResetModel，此时会触发modelAboutToBeReset信号
    beginResetModel();

    m_hor_hedlbls = map["headerlabel"].toStringList();
    m_table_map = map;
    m_vec_hedlbls = map.keys();

    //数据设置结束后调用endResetModel，此时会触发modelReset信号
    endResetModel();

    //如果表的行列数是固定的，只是数据变更了，我们可以用 dataChanged 信号来请求刷新
    //emit dataChanged(index(0,0),index(RowMax-1,ColMax-1),QVector<int>());
}

void TableModel::setHignlightIndex(QVector<QPair<int, int> > vec_index)
{
    beginResetModel();
    m_highlight_indexs = vec_index;
    endResetModel();
}

Qt::ItemFlags TableModel::flags(const QModelIndex &index) const
{
    Qt::ItemFlags flags = QAbstractItemModel::flags(index);
    flags |= Qt::ItemIsEditable;
    return flags;
}

int TableModel::rowCount(const QModelIndex &parent) const
{
    if(parent.isValid())
        return 0;
    else
        return m_table_map.size();

}

int TableModel::columnCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return 0;
    else
        return m_hor_hedlbls.size();
}

QVariant TableModel::data(const QModelIndex &index, int role) const
{
   if(role == Qt::DisplayRole || role == Qt::EditRole)
   {
       QVariantMap *map = (QVariantMap*)(m_table_map[m_vec_hedlbls[index.row()]].data());
       return (*map)[m_hor_hedlbls[index.column()]];
   }
   else if(role == Qt::TextAlignmentRole)
       return Qt::AlignCenter;

   return QVariant();
}

bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (index.isValid() && role == Qt::EditRole)
    {
        QVariantMap *_map = (QVariantMap*)(m_table_map[m_vec_hedlbls[index.row()]].data());
        if((*_map)[m_hor_hedlbls[index.column()]] != value)
        {
            (*_map)[m_hor_hedlbls[index.column()]] = value;
            emit dataChanged(index, index);
            emit itemChanged(index, value);

            return true;
        }
    }
    return false;
}

QString TableModel::itemText(int row, int column) const
{
    QVariantMap *_map = (QVariantMap*)(m_table_map[m_vec_hedlbls[row]].data());
    return (*_map)[m_hor_hedlbls[column]].toString();
}

QString TableModel::itemText(const QModelIndex &index) const
{
    QVariantMap *_map = (QVariantMap*)(m_table_map[m_vec_hedlbls[index.row()]].data());
    return (*_map)[m_hor_hedlbls[index.column()]].toString();
}

void TableModel::setItemText(const QModelIndex &index, const QString &str)
{
    QVariantMap *_map = (QVariantMap*)(m_table_map[m_vec_hedlbls[index.row()]].data());
    (*_map)[m_hor_hedlbls[index.column()]] = str;
}

void TableModel::setItemText(int row, int column, const QString &str)
{
    QVariantMap *_map = (QVariantMap*)(m_table_map[m_vec_hedlbls[row]].data());
    (*_map)[m_hor_hedlbls[column]] = str;
}

QVariant TableModel::ItemData(const QModelIndex &index) const
{
    QVariantMap *_map = (QVariantMap*)(m_data_map[m_vec_hedlbls[index.row()]].data());
    return (*_map)[m_hor_hedlbls[index.column()]];
}

QVariant TableModel::ItemData(int row, int column) const
{
    QVariantMap *_map = (QVariantMap*)(m_data_map[m_vec_hedlbls[row]].data());
    return (*_map)[m_hor_hedlbls[column]];
}

void TableModel::SetItemData(const QModelIndex &index, const QVariant &data)
{
    QString vec_lbl = m_vec_hedlbls[index.row()];
    if(!m_data_map.contains(vec_lbl))   m_data_map[vec_lbl] = QVariantMap();

    QVariantMap *_map = (QVariantMap*)(m_data_map[vec_lbl].data());
    (*_map)[m_hor_hedlbls[index.column()]] = data;

}

void TableModel::SetItemData(int row, int column, const QVariant &data)
{
    QString vec_lbl = m_vec_hedlbls[row];
    if(!m_data_map.contains(vec_lbl))   m_data_map[vec_lbl] = QVariantMap();

    QVariantMap *_map = (QVariantMap*)(m_data_map[vec_lbl].data());
    (*_map)[m_hor_hedlbls[column]] = data;
}

void TableModel::clear()
{
    beginResetModel();
    m_table_map.clear();
    m_data_map.clear();
    m_hor_hedlbls.clear();
    m_vec_hedlbls.clear();
    m_highlight_indexs.clear();
    endResetModel();
}

QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal)
    {
        if (role == Qt::DisplayRole)    return m_hor_hedlbls.at(section);
        else    return QVariant();
    }

    return QAbstractTableModel::headerData(section, orientation, role); // 垂直表头的序号
}
